# $Id$
# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Thomas Baechler <thomas@archlinux.org>

pkgbase=nvidia-304xx
pkgname=(nvidia-304xx nvidia-304xx-dkms)
pkgver=304.137
_extramodules=extramodules-4.13-ARCH
pkgrel=2
arch=('i686' 'x86_64')
url="http://www.nvidia.com/"
makedepends=('linux' 'linux-headers>=4.13' 'linux-headers<4.14')
conflicts=('nvidia')
license=('custom')
options=('!strip')
source=('disable-mtrr.patch')
source_i686+=("http://us.download.nvidia.com/XFree86/Linux-x86/${pkgver}/NVIDIA-Linux-x86-${pkgver}.run")
source_x86_64+=("http://us.download.nvidia.com/XFree86/Linux-x86_64/${pkgver}/NVIDIA-Linux-x86_64-${pkgver}-no-compat32.run")
sha512sums=('54e8825e523f11706890e09e476498f3a30f75ce4e350ff2ff8a1e1c3af574d9ccfb8903543766b6863d94cdfbf46e68cd3d33380867dd976cafc8dd2dd78774')
sha512sums_i686=('021c5a88d50489aa0a1c7eac14259e23542b93578ae339995a2e38b786d06d239968c07933cab3fb78a922cffc7e213d6fa3fe07e34065a9c7f03e24f8729370')
sha512sums_x86_64=('4ab648647e4f3e2c352b2eab6454c264fe4728d8eb1264fafee2a4ab1a4ce59516abcf8490044d31e35309ea951e564cc217e13f7139a48bbf650a4238c5b87c')

[[ "$CARCH" = "i686" ]] && _pkg="NVIDIA-Linux-x86-${pkgver}"
[[ "$CARCH" = "x86_64" ]] && _pkg="NVIDIA-Linux-x86_64-${pkgver}-no-compat32"

prepare() {
    cd "${srcdir}"
    sh "${_pkg}.run" --extract-only
    cd "${_pkg}"

    # FS#47092
    #(cd kernel; patch -p1 --no-backup-if-mismatch -i "$srcdir"/disable-mtrr.patch)

    cp -a kernel kernel-dkms
}

build() {
    _kernver="$(cat /usr/lib/modules/${_extramodules}/version)"
    cd "${_pkg}/kernel"
    make SYSSRC=/usr/lib/modules/"${_kernver}/build" module
}

package_nvidia-304xx() {
    pkgdesc="NVIDIA drivers for linux, 304xx legacy branch"
    depends=('linux>=4.13' 'linux<4.14' 'libgl' "nvidia-304xx-utils=${pkgver}")
    conflicts+=('nvidia-304xx-dkms')
    install=nvidia-304xx.install

    install -D -m644 "${srcdir}/${_pkg}/kernel/nvidia.ko" \
        "${pkgdir}/usr/lib/modules/${_extramodules}/nvidia.ko"
    install -d -m755 "${pkgdir}/usr/lib/modprobe.d"
    echo "blacklist nouveau" >> "${pkgdir}/usr/lib/modprobe.d/nvidia.conf"
    sed -i -e "s/EXTRAMODULES='.*'/EXTRAMODULES='${_extramodules}'/" "${startdir}/${pkgname}.install"
    gzip "${pkgdir}/usr/lib/modules/${_extramodules}/nvidia.ko"

    # the license file is part of nvidia-304xx-utils - the module depends on it, so we don't ship it another time.
}

package_nvidia-304xx-dkms() {
    pkgdesc="NVIDIA driver sources for linux, 304xx legacy branch"
    depends=('dkms' "nvidia-304xx-utils=$pkgver")
    optdepends=('linux-headers: Build the module for Arch kernel'
                'linux-lts-headers: Build the module for LTS Arch kernel')
    conflicts+=('nvidia-304xx')

    cd ${_pkg}
    make -C kernel clean
    install -dm 755 "${pkgdir}"/usr/{lib/modprobe.d,src}
    cp -dr --no-preserve='ownership' kernel-dkms "${pkgdir}"/usr/src/nvidia-${pkgver}
    echo 'blacklist nouveau' > "${pkgdir}"/usr/lib/modprobe.d/nvidia.conf
}
